package com.shigu.admin.manager.actions;

import com.opentae.data.mall.beans.ManagerUser;
import com.shigu.admin.manager.bo.*;
import com.shigu.admin.manager.common.CustomPager;
import com.shigu.admin.manager.common.JSONResult;
import com.shigu.admin.manager.services.AdminUserService;
import com.shigu.admin.manager.vo.UserRoleVO;
import com.shigu.admin.manager.vo.UserVO;
import com.shigu.annotations.UserOption;
import com.shigu.component.enums.SessionEnum;
import com.shigu.component.shiro.AuthorityUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户角色权限管理
 * Created by zhaohongbo on 16/5/12.
 */
@Controller
public class AdminUserAction extends BaseAction{


    @Autowired
    AdminUserService adminUserService;

    /**
     * 用户管理
     * @return
     */
    @RequestMapping("userlist")
    @RequiresPermissions("user:list")
    public String user(PageSearchBO bo,Model model,HttpServletRequest request){
        if(bo.getPageNo()==null){
            bo.setPageNo(1);
        }
        model.addAttribute("keyword",bo.getKeyword());
        model.addAttribute("pageNo",bo.getPageNo());
        CustomPager<UserVO> pager=adminUserService.selUsers(bo.getKeyword(),request.getHeader("Host"),bo.getPageNo(), PageSearchBO.pageSize,null);
        model.addAttribute("pager",pager);
        return "sys/userlist";
    }

    /**
     * 添加用户
     * @return
     */
    @RequestMapping("adduser")
    @RequiresPermissions("user:add")
    public String adduser(AddUserBO bo,HttpServletRequest request,Model model){
        String targetView="sys/adduser";
        String result=null;
        if(bo.getSubmit()==null){
            return targetView;
        }
        if((bo.getNick()==null||"".equals(bo.getNick()))&&bo.getSubmit()!=null&&bo.getSubmit()==1){
            result="nick";
            model.addAttribute("result",result);
            return targetView;
        }
        if((bo.getUsername()==null||"".equals(bo.getUsername()))&&bo.getSubmit()!=null&&bo.getSubmit()==1){
            result="username";
            model.addAttribute("result",result);
            return targetView;
        }
        if((bo.getPassword()==null||"".equals(bo.getPassword()))&&bo.getSubmit()!=null&&bo.getSubmit()==1){
            result="password";
            return targetView;
        }
        String msg=null;
        List<ManagerUser> oldusers=adminUserService.checkNameAndNick(bo.getNick(), bo.getUsername(),request.getHeader("Host"));
        if(oldusers.size()==0){//可以用
            Long userId=adminUserService.addUser(bo.getNick(),bo.getUsername(),bo.getPassword(),request.getHeader("Host"));
            msg="添加成功,userId:"+userId;
            bo.setNick(null);
            bo.setUsername(null);
            bo.setPassword(null);
            model.addAttribute("msg",msg);
            return targetView;
        }else{
            for(ManagerUser mu:oldusers){
                if(mu.getNick().equals(bo.getNick())){
                    result="nick";
                    msg="用户昵称已经被使用";
                    model.addAttribute("msg",msg);
                    return targetView;
                }else if(mu.getLoginName().equals(bo.getUsername())){
                    result="username";
                    msg="用户账号已经存在";
                    model.addAttribute("msg",msg);
                    return targetView;
                }
            }
        }
        return targetView;
    }

    /**
     * 修改用户状态
     * @return
     */
    @RequestMapping(value = "cguser")
    @ResponseBody
    public JSONResult cguser(Long uid){
        if(uid==null){
            return JSONResult.error();
        }
        int result=adminUserService.changeUserStatus(uid);
        if(result==0){
            return JSONResult.error();
        }else{
            return JSONResult.success();
        }
    }
    /**
     * 修改用户角色
     * @return
     */
    @RequestMapping("modusole")
    @RequiresPermissions("user:modify")
    public String modusole(ModusoleBO bo, Model model,HttpServletRequest request){
        if(bo.getPageNo()==null){
            bo.setPageNo(1);
        }
        model.addAttribute("keyword",bo.getKeyword());
        model.addAttribute("pageNo",bo.getPageNo());
        CustomPager<UserVO> pager=adminUserService.selUsers(bo.getKeyword(),request.getHeader("Host"),bo.getPageNo(), PageSearchBO.pageSize,1);
        model.addAttribute("pager",pager);
        model.addAttribute("uid",bo.getUid());
        //查出角色列表
        List<UserRoleVO> urvlist=adminUserService.selRoleAll(bo.getUid());
        model.addAttribute("urvlist",urvlist);
        return "sys/changeuser";
    }

    /**
     * 修改密码
     * @return
     */
    @RequestMapping("modpwd")
    public String modpwd(ModpwdBO bo,Model model){
        String targetView="sys/moduser";
            if(bo.getUid()==null){
            return targetView;
        }
        model.addAttribute("uid",bo.getUid());
        model.addAttribute("password",bo.getPassword());
        model.addAttribute("submit",bo.getSubmit());
        UserVO uvo=adminUserService.seluserByid(bo.getUid());
        if(uvo!=null){
            bo.setUsername(uvo.getUserName());
            bo.setNick(uvo.getNick());
        }else{
            return ERROR500;
        }
        model.addAttribute("nick",bo.getNick());
        model.addAttribute("username",bo.getUsername());
        if(bo.getSubmit()!=null&&bo.getSubmit()==1&&bo.getPassword()!=null){
            adminUserService.modpwd(bo.getUid(),bo.getPassword());
            String msg="修改成功";
            model.addAttribute("msg",msg);
        }
        return targetView;
    }

    /**
     * 修改我的密码
     * @return
     */
    @RequestMapping("modmypwd")
    @RequiresPermissions("system:mypwd")
    public String modmypwd(ModpwdBO bo,HttpSession session,Model model){
        String targetView="sys/moduser";
        AuthorityUser user= (AuthorityUser) session.getAttribute(SessionEnum.LOGIN_USER.toString());
        bo.setUid(user.getUserId());
        model.addAttribute("uid",user.getUserId());
        model.addAttribute("password",bo.getPassword());
        model.addAttribute("submit",bo.getSubmit());
        UserVO uvo=adminUserService.seluserByid(user.getUserId());
        if(uvo!=null){
            bo.setUsername(uvo.getUserName());
            bo.setNick(uvo.getNick());
        }else{
            return ERROR500;
        }
        model.addAttribute("nick",bo.getNick());
        model.addAttribute("username",bo.getUsername());
        if(bo.getSubmit()!=null&&bo.getSubmit()==1&&bo.getPassword()!=null){
            adminUserService.modpwd(bo.getUid(),bo.getPassword());
            String msg="修改成功";
            model.addAttribute("msg",msg);
        }
        return targetView;
    }

    /**
     * 修改角色
     * @return
     */
    @RequestMapping("obrole")
    @ResponseBody
    public JSONResult obrole(ObroleBO bo){
        int result = adminUserService.changeRole(bo.getUid(),bo.getRid());
        obj= JSONResult.success("" + result);
        return obj;
    }

    public Integer getLeftflag() {
        return leftflag;
    }

    public void setLeftflag(Integer leftflag) {
        this.leftflag = leftflag;
    }

}
